 global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victimalt_data_clean_new", clear
g alt_rel=1
append using "W:\intimate\data\match_victim_data_clean_new"
foreach thing in alt_rel victim_alt dv_couple {
	replace `thing' = 0 if missing(`thing')
}
replace victim_alt = 1 if dv_couple==1
egen match_id_int = group(match_id alt_rel)

g before_dv = (year_start_cohab < first_dv_year)
bys match_id_int year_start_cohab : egen before_dv_match = min(before_dv)

g noncontam_sample = 1
replace noncontam_sample = 0 if alt_rel==1 & before_dv_match==0

g av_prior_inc = (tyotuB3 + tyotuB2 + tyotuB1)/3

local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n

reshape long `varlist', i(group_id) j(time)

// generate treatment variables
g victim_full = 0
replace victim_full = 1 if dv_couple==1
replace victim_full = 1 if victim_alt==1

g abusive_full = 0 
replace abusive_full = 1 if alt_rel==0 

drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat_vic = victim_full==1
gen treat_ab = abusive_full==1
g treat_vic_ab = victim_full==1 & abusive_full==1

gen treatPost_vic= treat_vic==1 & time_>=0
gen treatPost_ab = treat_ab==1 & time_>=0
gen treatPost_vic_ab = treat_vic==1 & treat_ab==1 & time_>=0
gen Post = time_>=0


foreach thing in time year_start_cohab {
    egen `thing'_int = group(`thing' alt_rel)
}
global fe_full = "match_id_int time_int year_start_cohab_int"
global cluster_full = "match_id_int"

*Time displacement dummies
foreach thing in vic ab vic_ab {
	g dpl_5`thing'=time_==-5 & treat_`thing'==1
	g dpl_4`thing'=time_==-4 & treat_`thing'==1
	g dpl_3`thing'=time_==-3 & treat_`thing'==1
	g dpl_2`thing'=time_==-2 & treat_`thing'==1
	g dpl_1`thing'=time_==-1 & treat_`thing'==1
	g dpl_0`thing'=time_==0 & treat_`thing'==1
	gen dpl1`thing'=time_==1 & treat_`thing'==1
	gen dpl2`thing'=time_==2 & treat_`thing'==1
	gen dpl3`thing'=time_==3 & treat_`thing'==1
	gen dpl4`thing'=time_==4 & treat_`thing'==1
	gen dpl5`thing'=time_==5 & treat_`thing'==1
	
	global dummies_`thing' =  "dpl_5`thing' dpl_4`thing' dpl_3`thing' dpl_2`thing' dpl_1`thing' dpl_0`thing' dpl1`thing' dpl2`thing' dpl3`thing' dpl4`thing' dpl5`thing'"
	
	
}




capture program drop eventStudyGraphs_triple
program define eventStudyGraphs_triple
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies_vic_ab $dummies_vic $dummies_ab i.time if noncontam_sample==1, ///
		absorb(match_id_int year_start_cohab_int)  cluster($cluster_full)
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i'vic_ab]  if t == -`i'
		cap	replace se_est =  _se[dpl_`i'vic_ab]  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i'vic_ab]  if t == `i'
		cap	replace se_est =  _se[dpl`i'vic_ab]  if t == `i'
	}

	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\victim_triplediff_`a'.pdf", replace
	restore
	}
end



eventStudyGraphs_triple "ptoim1" "-0.1(0.02)0.02" "Employment" 
eventStudyGraphs_triple "tyotu" "-2500(500)1000" "Earnings" 


capture program drop eventStudyGraphs_triple
program define eventStudyGraphs_triple
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies_vic_ab $dummies_vic $dummies_ab i.time , ///
		absorb(match_id_int year_start_cohab_int)  cluster($cluster_full)
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i'vic_ab]  if t == -`i'
		cap	replace se_est =  _se[dpl_`i'vic_ab]  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i'vic_ab]  if t == `i'
		cap	replace se_est =  _se[dpl`i'vic_ab]  if t == `i'
	}

	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\victim_triplediff_`a'_norestrict.pdf", replace
	restore
	}
end



eventStudyGraphs_triple "ptoim1" "-0.1(0.02)0.02" "Employment" 
eventStudyGraphs_triple "tyotu" "-2500(500)1000" "Earnings" 



